package com.kamkor.interviewer.client.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Label;
import com.kamkor.interviewer.client.Interviewer;
import com.kamkor.interviewer.client.presenter.EditInterviewPresenter;
import com.kamkor.interviewer.client.presenter.EditInterviewPresenter.Display;
import com.kamkor.interviewer.shared.QuestionAnswerDetails;

/**
 * 
 * @author kamkor
 *
 */
public class EditInterviewView extends Composite implements Display {
	interface MyUiBinder extends UiBinder<Widget, EditInterviewView> {}
	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	FlexTable questionAnswerTable;
	
	@UiField
	Button addQuestion;
	
	@UiField 
	Button saveAll;
	
	@UiField
	Button merge;
	
	@UiField
	CheckBox minimizedAllQuestionAnswer;
	
	@UiField 
	CheckBox showDeleteButtons;
	
	@UiField 
	Label isSavedInfo;	
	
	@UiField
	TextArea addQuestionArea;
	
	private ChangeHandler questionAnswerTextChangeHandler;
	
	private ClickHandler deleteQuestionAnswerButtonsHandler;
	
	private LinkedList<DeleteQuestionAnswerButton> deleteButtons = new LinkedList<DeleteQuestionAnswerButton>();
	
	public EditInterviewView() {
		initWidget(uiBinder.createAndBindUi(this));		
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public FlexTable getQuestionAnswerTable() {
		return questionAnswerTable;
	}

	@Override
	public Button getAddQuestion() {
		return addQuestion;		
	}

	@Override
	public HasText getAddQuestionArea() {		
		return addQuestionArea;		
	}
	
	@Override
	public HasClickHandlers getSaveAll() {
		return saveAll;		
	}

	@Override
	public HasClickHandlers getMerge() {
		return merge;
	}
	
	@Override
	public List<QuestionAnswerDetails> getData(Long interviewId) {
		final int rowCount = questionAnswerTable.getRowCount();
		List<QuestionAnswerDetails> list = new ArrayList<QuestionAnswerDetails>(rowCount);
		
		// Disable buttons during data retrieval
		addQuestion.setEnabled(false);
		merge.setEnabled(false);
		for(int i = 0; i < rowCount; i++) {			
			VerticalPanel panel = (VerticalPanel) ((DisclosurePanel) questionAnswerTable.getWidget(i, 0)).getContent();
			Label questionId = (Label) panel.getWidget(0);
			Label questionPosition = (Label) panel.getWidget(1);	
			TextArea question = (TextArea) panel.getWidget(2);			
			TextArea answer = (TextArea) ((DisclosurePanel) panel.getWidget(3)).getContent();				
			
			QuestionAnswerDetails qaDetails = new QuestionAnswerDetails(
					new Long(questionId.getText()), interviewId, question.getText(), answer.getText());
			
			qaDetails.setPositionNumber(new Long(questionPosition.getText()));
			list.add(qaDetails);
		}	
		// Enable buttons
		addQuestion.setEnabled(true);
		merge.setEnabled(true);
		return list;
	}

	@Override
	public void setData(List<QuestionAnswerDetails> list) {
		questionAnswerTable.clear();
		for(int row = 0; row < list.size(); row++) {
			QuestionAnswerDetails questionAnswer = list.get(row);
			addToData(questionAnswer);
		}
	}

	@Override
	public void addToData(QuestionAnswerDetails questionAnswer) {
		// invisible label that contains question id
		Label questionId = new Label(questionAnswer.getId().toString());
		questionId.setVisible(false);
		
		// invisible label that contains question position
		Label questionPosition = new Label(questionAnswer.getPositionNumber().toString());
		questionPosition.setVisible(false);			

		// question text area
		final TextArea question = new TextArea();
		question.setText(questionAnswer.getQuestion());
		question.setStyleName("gwt-RichTextArea-question");
		question.addChangeHandler(questionAnswerTextChangeHandler);
		// answer text area
		final TextArea answer = new TextArea();
		answer.setText(questionAnswer.getAnswer());						
		answer.addStyleName("gwt-RichTextArea-answer");		
		answer.addChangeHandler(questionAnswerTextChangeHandler);
		
		// Disclosure panel for text area
		final DisclosurePanel disclosureAnswerPanel = new DisclosurePanel(Interviewer.constants.showOrHideAnswer());
		disclosureAnswerPanel.setAnimationEnabled(true);
		disclosureAnswerPanel.add(answer);
		
		// each question answer has a delete button
		final DeleteQuestionAnswerButton deleteButton = new DeleteQuestionAnswerButton(questionAnswer.getId());
		deleteButton.addClickHandler(this.deleteQuestionAnswerButtonsHandler);
		deleteButton.setVisible(isShowDeleteButtons());	
		
		// header panel of question answer, when clicked it opens or closes question answer panel. 
		// also has delete button on far right
		final HorizontalPanel headerPanel = new HorizontalPanel();
		headerPanel.setStyleName("questionAnswerHeader");			
		headerPanel.add(new Label(questionAnswer.getQuestionShort()));
		headerPanel.add(deleteButton);
		headerPanel.setCellHorizontalAlignment(deleteButton, HasHorizontalAlignment.ALIGN_RIGHT);
		
		// question answer panel
		VerticalPanel questionAnswerPanel = new VerticalPanel();	
		questionAnswerPanel.add(questionId);
		questionAnswerPanel.add(questionPosition);	
		questionAnswerPanel.add(question);	
		questionAnswerPanel.add(disclosureAnswerPanel);	
		
		// QuestionAnswer disclosurePanel		
		DisclosurePanel disclosureQuestionAnswerPanel = new DisclosurePanel();
		disclosureQuestionAnswerPanel.setStyleName("qaPanel");
		disclosureQuestionAnswerPanel.setAnimationEnabled(true);
		disclosureQuestionAnswerPanel.setHeader(headerPanel);			
		disclosureQuestionAnswerPanel.add(questionAnswerPanel);		
		
		final int newRow = questionAnswerTable.getRowCount();
		questionAnswerTable.setWidget(newRow, 0, disclosureQuestionAnswerPanel);		
	}

	@Override
	public void deleteQuestionAnswerPanel(Long id, PushButton deleteButton) {
		final int rowCount = questionAnswerTable.getRowCount();
		boolean questionAnswerFound = false;		
		int row = 0;
		
		while(!questionAnswerFound && row < rowCount) {
			VerticalPanel panel = (VerticalPanel) ((DisclosurePanel) questionAnswerTable.getWidget(row, 0)).getContent();
			Label questionIdLabel = (Label) panel.getWidget(0);
			Long questionId = new Long(questionIdLabel.getText());
			if (questionId.equals(id)) {
				questionAnswerTable.removeRow(row);
				deleteButtons.remove(deleteButton);
				questionAnswerFound = true;
			}		
			row++;
		}
	}

	@Override
	public void setQuestionAnswerTextChangeHandler(ChangeHandler handler) {
		this.questionAnswerTextChangeHandler = handler;		
	}

	@Override
	public Label getIsSavedInfo() {
		return isSavedInfo;
	}
	
	/**
	 * This handler is closely related to view, presenter doesn't have to implement or know about it
	 */
	@UiHandler("minimizedAllQuestionAnswer")
	void handleMinimizeCheckBoxClick(ClickEvent e) {
		final int rowCount = questionAnswerTable.getRowCount();
		for(int row = 0; row < rowCount; row++) {
			DisclosurePanel panel = (DisclosurePanel) questionAnswerTable.getWidget(row, 0);			
			panel.setOpen(!isMinimizedAllQuestionAnswer());
		}		
	}
	
	/**
	 * This handler is closely related to view, presenter doesn't have to implement or know about it.
	 * Shows/hides delete buttons
	 */
	@UiHandler("showDeleteButtons")
	void handleShowDeleteButtonsClick(ClickEvent e) {
		for (PushButton button : deleteButtons) {
			button.setVisible(isShowDeleteButtons());
		}
	}
	
	boolean isMinimizedAllQuestionAnswer() {		
		return minimizedAllQuestionAnswer.getValue();				
	}
	
	boolean isShowDeleteButtons() {
		return showDeleteButtons.getValue();
	}	

	@Override
	public void setDeleteQuestionAnswerButtonsHandler(ClickHandler handler) {
		this.deleteQuestionAnswerButtonsHandler = handler;		
	}

	@Override
	public boolean isAutoSave() {
		// TODO Auto-generated method stub
		return false;
	}
	
}
